package model;
import java.util.ArrayList;

public class SaelgerCont
{
    ArrayList<Saelger> saelgere;
    private static SaelgerCont instance;

    public SaelgerCont()
    {
        saelgere = new ArrayList<Saelger>();
    }

    public static SaelgerCont getInstance()
    {
        if(instance == null)
        {
            instance = new SaelgerCont();
        }
        return instance;
    }

    public int addSaelger(Saelger s)
    {
        int nyId = 1;
        if (s.getSaelgerId() == 0)
        {
            if(saelgere.size() > 0)
            {
                int lastIndex = saelgere.size()-1;
                Saelger lastSaelger = saelgere.get(lastIndex);
                nyId = lastSaelger.getSaelgerId();
                nyId += 1;
            }
            s.setSaelgerId(nyId);
        }
        saelgere.add(s);
        return nyId;
    }

    public Saelger findSaelger(int id)
    {
        Saelger saelger = null;
        int index = 0;
        boolean found = false;
        while (index < saelgere.size() && !found)
        {
            saelger = saelgere.get(index);
            if(saelger.getSaelgerId() == id)
            {
                found = true;
            }
            else
            {
                index++;
            }
            if (found)
            {
                return saelgere.get(index);
            }
        }
        return null;
    }
    public void sletSaelger(int saelgerID)
    {
        Saelger saelger = findSaelger(saelgerID);
        saelgere.remove(saelger);
    }
}

